package com.tj.shopping.web.controller;

import com.tj.shopping.entity.Carts;
import com.tj.shopping.service.ShoppingCartService;
import com.tj.shopping.service.impl.ShoppingCartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: H
 * @create: 2023-12-20 19:28
 */
@WebServlet("/cart")
public class ShoppingCartController extends BaseController {
    private ShoppingCartService service = new ShoppingCartServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String op = req.getParameter("op");
        System.out.println(op);
        if ("finds".equals(op)) {
            doFinds(req, resp);

        } else if ("remove".equals(op)) {
            doRemove(req, resp);
        } else if ("removeByCno".equals(op)) {
            doRremoveByCno(req, resp);
        } else if ("deleteAll".equals(op)) {
            doDeleteAll(req, resp);
        } else if ("addnum".equals(op)) {
            doAddNum(req, resp);
        } else if ("addshoppingcart".equals(op)) {
            doaddshoppingcart(req, resp);
        } else if ("carttotal".equals(op)) {
            docarttotal(req, resp);
        } else if ("selectCart".equals(op)) {
            doSelectCart(req, resp);
        } else {
            resp.getWriter().print("no such method");
        }


    }

    protected void doRremoveByCno(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int cno = Integer.parseInt(req.getParameter("cno"));
        int result = service.RemoveByCno(cno);
        printToJson(resp, result);

    }


    protected void doSelectCart(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Carts carts = parseRequest(req, Carts.class);
        System.out.println(carts);
        Carts cart = service.selectCart(carts.getCno());
        printToJson(resp, cart);
    }

    protected void doFinds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String, Object> m = parseRequest(req);

        Map<String, Object> map = service.finds(m);

        printToJson(resp, map);
    }

    protected void doRemove(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("删除");
        Map<String, Object> map = parseRequest(req);
        int result = service.Remove(map);

        printToJson(resp, result);

    }

    protected void doDeleteAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("删除");
        Map<String, Object> map = parseRequest(req);
        int result = service.deleteAll(map);
        if (result > 0) {
            printToJson(resp, result);
        } else {
            printToJson(resp, result);
        }
    }

    protected void doAddNum(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Carts carts = parseRequest(req, Carts.class);
        Map<String, Object> map = parseRequest(req);
        int result = service.addNum(carts, map);
        if (result > 0) {
            printToJson(resp, result);
        } else {
            printToJson(resp, result);
        }


    }


    protected void doaddshoppingcart(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Map<String, Object> map = parseRequest(req);
        map.put("uno", Integer.parseInt((String) map.get("uno")));
        map.put("number", Integer.parseInt((String) map.get("number")));
        map.put("gdno", Integer.parseInt((String) map.get("gdno")));
        System.out.println();
        Map<String, Object> map1 = service.addshoppingcart(map);
        printToJson(resp, map1);
    }

    protected void docarttotal(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Map<String, Object> map = parseRequest(req);
        map.put("uno", Integer.parseInt((String) map.get("uno")));
        Map<String, Object> map1 = service.carttotal(map);
        printToJson(resp, map1);
    }

}
